package com.zx.mobile.controller;

import com.zx.map.service.MerchantInfoService;
import com.zx.map.service.MessageSendLogService;
import com.zx.map.service.model.MerchantInfoModel;
import com.zx.mobile.MobileCommonHelper;
import com.zx.mobile.SessionHelper;
import com.zx.mobile.domain.GetPasswordDomain;
import com.zx.mobile.domain.MobileUserDomain;
import com.zx.mobile.domain.PwdModifyDomain;
import com.zx.mobile.domain.pwd.PayPwdDomain;
import com.zx.platform.utils.MD5Util;
import com.zx.platform.utils.PmpUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by IntelliJ IDEA. User: Administrator Date: 15-7-5 Time: 下午8:09 To
 * change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/mobile/index")
public class MobilePasswordController {
    /**
     * log
     */
    private static Logger log = Logger.getLogger(MobilePasswordController.class);
    @Autowired
    private MerchantInfoService merchantInfoService;
    @Autowired
    private MessageSendLogService messageSendLogService;

    /**
     * 忘记密码
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/getpassword")
    public String getpassword(Model model, HttpServletRequest request) {
        try {
            return "/jsp/mobile/getpassword";
        } catch (Exception e) {
            model.addAttribute("errorMsg", "系统异常");
            log.error("系统异常", e);
            return "/jsp/mobile/error";
        }
    }

    /**
     * 密码重置 成功页
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/getpwdresult")
    public String getpwdresult(GetPasswordDomain getPasswordDomain,
                               Model model, HttpServletRequest request) {
        try {
            MerchantInfoModel param = new MerchantInfoModel();
            param.setMerchantId(getPasswordDomain.getMerchantId());
            param.setSysAccount(getPasswordDomain.getTelephone());
            param.setSysPassword(PmpUtils.encoderByMD5(MD5Util
                    .getMD5Str(getPasswordDomain.getNewPwd())));
            merchantInfoService.update(param);
            model.addAttribute("operMsg", "密码重置成功");
            return "/jsp/mobile/getpwdsuccess";
        } catch (Exception e) {
            model.addAttribute("errorMsg", "系统异常");
            log.error("系统异常", e);
            return "/jsp/mobile/error";
        }
    }

    /**
     * 忘记密码第二步
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/getpwdsecond")
    public String getpwdsecond(GetPasswordDomain getPasswordDomain,
                               Model model, HttpServletRequest request) {
        try {
            MerchantInfoModel temp = merchantInfoService
                    .getMerchantInfoModelBySysAccount(getPasswordDomain
                            .getTelephone());
            if (null != temp) {
                if (!messageSendLogService.checkVerifyCode(
                        getPasswordDomain.getTelephone(),
                        getPasswordDomain.getVerifyCode(),
                        MobileCommonHelper.MESSAGETYPE_FORGETPWD)) {
                    model.addAttribute("errorMsg", "验证码不正确");
                    return "/jsp/mobile/error";
                } else {
                    getPasswordDomain.setMerchantId(temp.getMerchantId());
                    model.addAttribute("model", getPasswordDomain);
                    return "/jsp/mobile/getpwdsecond";
                }
            } else {
                model.addAttribute("errorMsg", "输入错误");
                return "/jsp/mobile/error";
            }
        } catch (Exception e) {
            model.addAttribute("errorMsg", "系统异常");
            log.error("系统异常", e);
            return "/jsp/mobile/error";
        }
    }

    /**
     * 修改登陆密码 流程1
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/changeLoginPwd")
    public String changeLoginPwd(Model model, HttpServletRequest request) {
        try {
            log.debug("修改登陆密码");
            return "/jsp/mobile/pwd/changeloginpwd";
        } catch (Exception e) {
            model.addAttribute("errorMsg", "系统异常");
            log.error("系统异常", e);
            return "/jsp/mobile/error";
        }
    }


    /**
     * 设置交易密码 第二步  设置交易密码
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/setPayPwd")
    public String confirmPayPwd(PayPwdDomain payPwdDomain, Model model, HttpServletRequest request) {
        try {
            log.debug("确认支付密码");
            MobileUserDomain user = (MobileUserDomain) request.getSession()
                    .getAttribute(SessionHelper.MOBILEUSER);//
            MerchantInfoModel param = new MerchantInfoModel();
            param.setMerchantId(user.getMerchantInfoDetailModel()
                    .getMerchantId());
            param.setTradePassword(PmpUtils.encoderByMD5(MD5Util
                    .getMD5Str(payPwdDomain.getPayPwd())));
            merchantInfoService.update(param);
            //设置跳转url
            String returnUrl = "";
            if (MobileCommonHelper.URLRETURN_MODIFYPWD.equals(payPwdDomain.getOperatorType())) {
                returnUrl = "/mobile/index/changePayPwd";
            } else if (MobileCommonHelper.URLRETURN_ENCHASH.equals(payPwdDomain.getOperatorType())) {
                returnUrl = "/mobile/index/enchash";
            } else if (MobileCommonHelper.URLRETURN_CREDIT.equals(payPwdDomain.getOperatorType())) {
                returnUrl = "/mobile/index/creditRepay";
            } else if (MobileCommonHelper.URLRETURN_SHAREPROFIT.equals(payPwdDomain.getOperatorType())) {
                returnUrl = "/mobile/sprofit/queryShareProfit";
            }
            model.addAttribute("returnUrl", returnUrl);
            return "/jsp/mobile/pwd/confirmpaypwd";
        } catch (Exception e) {
            model.addAttribute("errorMsg", "系统异常");
            log.error("系统异常", e);
            return "/jsp/mobile/error";
        }
    }


    /**
     * 修改交易密码 流程1
     *
     * @param model
     * @param request
     * @return
     */
    @RequestMapping("/changePayPwd")
    public String changePayPwd(Model model, HttpServletRequest request) {
        try {
            log.debug("修改支付密码");
            MobileUserDomain user = (MobileUserDomain) request.getSession()
                    .getAttribute(SessionHelper.MOBILEUSER);//
            //获取最新数据
            MerchantInfoModel currentModel = merchantInfoService.getModelByKey(user.getMerchantInfoDetailModel().getMerchantId());
            //交易密码不为空的情况下  允许用户进行设置密码
            //若交易密码为空 需要用户先设置密码
            if (StringUtils.isNotBlank(currentModel.getTradePassword())) {
                return "/jsp/mobile/pwd/changepaypwd";
            } else {
                model.addAttribute("operatorType", MobileCommonHelper.URLRETURN_MODIFYPWD);//设置交易密码 回退到修改交易密码
                return "/jsp/mobile/pwd/setpaypwd";
            }
        } catch (Exception e) {
            model.addAttribute("errorMsg", "系统异常");
            log.error("系统异常", e);
            return "/jsp/mobile/error";
        }
    }

    /**
     * 密码修改
     *
     * @param pwdModifyDomain
     * @param request
     * @return
     */
    @RequestMapping("/modifypwd")
    public String modifypwd(PwdModifyDomain pwdModifyDomain, Model model,
                            HttpServletRequest request) {
        try {
            log.debug("密码修改："
                    + ToStringBuilder.reflectionToString(pwdModifyDomain));
            MobileUserDomain user = (MobileUserDomain) request.getSession()
                    .getAttribute(SessionHelper.MOBILEUSER);//
            //获取最新数据 
            MerchantInfoModel currentModel = merchantInfoService.getModelByKey(user.getMerchantInfoDetailModel().getMerchantId());
            if (StringUtils.isBlank(pwdModifyDomain.getConfirmPwd())
                    || StringUtils.isBlank(pwdModifyDomain.getNewPwd())
                    || StringUtils.isBlank(pwdModifyDomain.getOrgPwd())) {
                model.addAttribute("errorMsg", "输入信息不完整");
                return "/jsp/mobile/pwderror";
            } else if (MobileCommonHelper.PWDTYPE_LOGIN.equals(pwdModifyDomain
                    .getPwdType())
                    && !PmpUtils.encoderByMD5(
                    MD5Util.getMD5Str(pwdModifyDomain.getOrgPwd()))
                    .equals(currentModel.getSysPassword())) {
                model.addAttribute("errorMsg", "原密码输入错误");
                return "/jsp/mobile/pwd/pwdloginerror";
            } else if (MobileCommonHelper.PWDTYPE_TRADE.equals(pwdModifyDomain
                    .getPwdType())
                    && !PmpUtils.encoderByMD5(
                    MD5Util.getMD5Str(pwdModifyDomain.getOrgPwd()))
                    .equals(currentModel.getTradePassword())) {
                model.addAttribute("errorMsg", "原密码输入错误");
                return "/jsp/mobile/pwd/pwdpayerror";
            } else {
                MerchantInfoModel param = new MerchantInfoModel();
                param.setMerchantId(user.getMerchantInfoDetailModel()
                        .getMerchantId());
                if (MobileCommonHelper.PWDTYPE_LOGIN.equals(pwdModifyDomain
                        .getPwdType())) {
                    param.setSysPassword(PmpUtils.encoderByMD5(MD5Util
                            .getMD5Str(pwdModifyDomain.getNewPwd())));
                } else {
                    param.setTradePassword(PmpUtils.encoderByMD5(MD5Util
                            .getMD5Str(pwdModifyDomain.getNewPwd())));
                }
                merchantInfoService.update(param);
                MobileControlHelper.reloadSessionInfoByMerchantId(merchantInfoService, user
                        .getMerchantInfoDetailModel().getMerchantId(), user.getKeepKey(), request);
                model.addAttribute("operMsg", "密码修改成功");
                return "/jsp/mobile/pwd/changepwdsuccess";
            }
        } catch (Exception e) {
            model.addAttribute("errorMsg", "系统异常");
            log.error("系统异常", e);
            return "/jsp/mobile/pwderror";
        }
    }
}
